package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductServiceImpl extends BaseService implements IProductService {
    @Override
    public void update(int id, int price) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.update(id,price);

        session.commit();
        session.close();
    }

    @Override
    public void addMore(List<ProductBean> productList) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.addMore(productList);

        session.commit();
        session.close();
    }

    @Override
    public CutPageBean<ProductBean> cutByItem(int pageNO, String name, String type, Integer startPrice, Integer endPrice) {
        CutPageBean<ProductBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.cutByItemList((pageNO - 1)*CutPageBean.PAGESIZE,
                CutPageBean.PAGESIZE,name,type,startPrice,endPrice);
        cutBean.setList(list);
        int count = mapper.cutByItemCount(name,type,startPrice,endPrice);
        cutBean.setCount(count);

        session.close();
        return cutBean;
    }
}
